package com.ruoyi.app.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.app.vo.CustomServiceVo;
import com.ruoyi.business.domain.JtAppUser;
import com.ruoyi.business.domain.JtBranchServices;
import com.ruoyi.business.domain.JtCustomerService;
import com.ruoyi.business.domain.JtUserAddress;
import com.ruoyi.business.service.IJtBranchServicesService;
import com.ruoyi.business.service.IJtCustomerServiceService;
import com.ruoyi.business.service.IJtUserAddressService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.JtImageUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 维修预约信息
 * @Author Alan Zhang
 * @Date 2025/3/15 13:21
 */
@RestController
@RequestMapping("/api/after/service")
public class ServicesApiController extends BaseApiController{
    @Autowired
    public IJtCustomerServiceService jtCustomerServiceService;
    @Autowired
    public IJtBranchServicesService jtBranchServicesService;

    @Autowired
    public IJtUserAddressService userAddressService;

    //========== 预约售后 =========
    @PostMapping("/submit")
    public AjaxResult submit(CustomServiceVo customServiceVo,
                             @RequestParam(value = "file", required = false) MultipartFile file1,
                             @RequestParam(value = "file2", required = false)MultipartFile file2) throws IOException {
        JtAppUser jtAppUser = getTokenAppUser();


        StringBuilder msgSb = new StringBuilder();
        if(StringUtils.isEmpty(customServiceVo.getDeviceModelNo())){
            msgSb.append("请填写设备型号!");
        }
        if(customServiceVo.getAddressId() == null){
            msgSb.append("请填写详细地址!");
        }
        if(StringUtils.isEmpty(customServiceVo.getProblem())){
            msgSb.append("请填写问题!");
        }
        if(customServiceVo.getBookTime() == null){
            msgSb.append("请填写预约时间!");
        }

        JtUserAddress jtUserAddress = userAddressService.selectJtUserAddressByAddressId(customServiceVo.getAddressId());
        if(jtUserAddress == null || !jtUserAddress.getUserId().equals(jtAppUser.getUserId())){
            msgSb.append("请填写预约地址");
        }
        if(msgSb.length() > 0){
            return AjaxResult.error(msgSb.toString());
        }

        JtCustomerService jtCustomerService = new JtCustomerService();
        jtCustomerService.setUserId(jtAppUser.getUserId());
        jtCustomerService.setEmail(jtAppUser.getEmail());
        jtCustomerService.setDeviceModelNo(customServiceVo.getDeviceModelNo());
        Date date = new Date(customServiceVo.getBookTime());
        jtCustomerService.setRepairTime(date);

        jtCustomerService.setCountryCode(jtUserAddress.getCountryCode());
        jtCustomerService.setPhoneNumber(jtUserAddress.getPhoneNumber());
        jtCustomerService.setFullAddress(jtUserAddress.getFullAddress());

        jtCustomerService.setProblem(customServiceVo.getProblem());
        if (file1 != null) {
            jtCustomerService.setImageUrl1(JtImageUtil.uploadService(file1, true));
        }
        if (file2 != null) {
            jtCustomerService.setImageUrl2(JtImageUtil.uploadService(file2, true));
        }
        int rows = jtCustomerServiceService.insertJtCustomerService(jtCustomerService);
        if(rows > 0){
            return AjaxResult.success("预约成功！");
        } else {
            return AjaxResult.error("预约失败！");
        }
    }

    //========== 预约列表 =========
    @PostMapping("/book/list")
    public AjaxResult bookList(Integer status, String startTime, String endTime)
    {
        JtAppUser jtAppUser = getTokenAppUser();

        JtCustomerService param = new JtCustomerService();
        param.setUserId(jtAppUser.getUserId());
        param.setRepairStatus(status);
        param.getParams().put("startTime", startTime);
        param.getParams().put("endTime", endTime);

        PageUtils.startPage();
        //排序字段
        PageHelper.orderBy("create_time desc");
        List<JtCustomerService> customerServiceList = jtCustomerServiceService.selectJtCustomerServiceList(param);
        TableDataInfo tableDataInfo = getDataTable(customerServiceList);
        return AjaxResult.success("success", tableDataInfo);
    }

    //========== 网点列表 =========
    @PostMapping("/outlets/list")
    public AjaxResult outletsList()
    {
        JtAppUser jtAppUser = getTokenAppUser();

        JtBranchServices param = new JtBranchServices();
        param.setCountryCode(jtAppUser.getCountryCode());

        PageUtils.startPage();
        //排序字段
        PageHelper.orderBy("create_time desc");
        List<JtBranchServices> branchServicesList = jtBranchServicesService.selectJtBranchServicesList(param);
        TableDataInfo tableDataInfo = getDataTable(branchServicesList);
        return AjaxResult.success("success", tableDataInfo);
    }
}
